package com.luntan.util.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.luntan.domain.TUser;

/**
 *
 * 用户登录管理监听器
 */
public class SessionAttributeListener implements HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        String name = event.getName();
        Object obj = event.getValue();
        if(obj instanceof TUser&&"UserInfo".equals(name)){
            HttpSession session = event.getSession();

            Map<String,String> map = (Map<String,String>) session.getServletContext().getAttribute("UserInfoMap");
            if(map == null){
               map = new HashMap<String,String>();
               session.getServletContext().setAttribute("UserInfoMap", map);
            }
            
            TUser user = (TUser) obj;
            map.put(user.getUserName(), session.getId());
            System.out.println("登录成功:"+user.getUserName()+"/"+session.getId());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
    }
    
}
